package com.fmsz.xmly.fragments.findfgts;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.fmsz.xmly.R;
import com.fmsz.xmly.activitys.classify.ClassifyTagActivity;
import com.fmsz.xmly.adapters.classify.tag.ClassifyAllTagAdapter;
import com.fmsz.xmly.adapters.classify.tag.ClassifyTagHotAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.callbacks.FindClassifyHttpService;
import com.fmsz.xmly.entitys.classify.tag.AllTagEntity;
import com.fmsz.xmly.entitys.classify.tag.ClassifyTagEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassifyTagBaseFragment extends Fragment implements Callback<ClassifyTagEntity> {

    private int tag;
    public static final int RECOMMEND = 0;
    public static final int NOMAL = 1;
    private TextView textView;
    private String tagName;
    private String categoryId;
    private RecyclerView recyclerView;
    private ClassifyTagHotAdapter first_adapter;
    private ClassifyAllTagAdapter normal_adapter;
    private ClassifyTagActivity tagActivity;

    public ClassifyTagBaseFragment() {
        // Required empty public constructor
    }

    public static ClassifyTagBaseFragment newInstance(String tagName, String categoryId, int tag) {
        Bundle args = new Bundle();
        args.putString("tagName",tagName);
        args.putString("categoryId",categoryId);
        args.putInt("tag",tag);
        ClassifyTagBaseFragment fragment = new ClassifyTagBaseFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        tagName = bundle.getString("tagName");
        tag = bundle.getInt("tag");
        categoryId = bundle.getString("categoryId");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_classify_tag_base, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        recyclerView = ((RecyclerView) view.findViewById(R.id.recycle_classify_tag));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        if (tag==RECOMMEND) {
            first_adapter = new ClassifyTagHotAdapter(getContext());
            recyclerView.setAdapter(first_adapter);
        }
        if(tag==NOMAL){
            normal_adapter = new ClassifyAllTagAdapter(getContext());
            recyclerView.setAdapter(normal_adapter);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        tagActivity = ((ClassifyTagActivity) getActivity());
        MyApplication application = (MyApplication) getActivity().getApplication();
        FindClassifyHttpService service = application.getClassifyService();
        if (tag==RECOMMEND) {
            service.getClassifyTagData(categoryId).enqueue(this);
        }
        if(tag==NOMAL){
            service.getAllTagData(categoryId,tagName).enqueue(new Callback<AllTagEntity>() {
                @Override
                public void onResponse(Call<AllTagEntity> call, Response<AllTagEntity> response) {
                    normal_adapter.addAll(response.body().getList());
                    if (tagActivity!=null) {
                        tagActivity.hideProgress();
                    }
                    tagActivity = null;
                }
                @Override
                public void onFailure(Call<AllTagEntity> call, Throwable t) {
                    tagActivity = null;
                    t.printStackTrace();
                    Toast.makeText(getContext(), "网络错误a", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onResponse(Call<ClassifyTagEntity> call, Response<ClassifyTagEntity> response) {
        if (response.body()!=null) {
            first_adapter.addAll(response.body());
            if (tagActivity!=null) {
                tagActivity.hideProgress();
            }
            tagActivity = null;
        }
    }

    @Override
    public void onFailure(Call<ClassifyTagEntity> call, Throwable t) {
        tagActivity = null;
    }
}
